﻿unit Res;

interface

uses
  Windows, Messages, SysUtils, Classes;

type
  TRes = class
  function GetBrowserEmbeddedObjectsFolder():String;
  function NewTabTxt():String;
  function NewProcTxt():String;
  function NewProcMonTxt():String;
  procedure SetupBrowserEmbeddedObjects();
  function TempFolder():String;
  procedure ResToFile(ResId:String;FileName:String);
  function FavHtm():String;
  function DocIco():String;
  function WbNodeExe():String;
  function Signature():String;
  function GetSystemRes(S: string): string;
  function s(S: string): string;
  function BrowserName():String;
  function NewTabUrl():String;
  function GetLocale():String;
  function GetText(id:String):String;
  constructor Create();
  end;

const
  Codes64 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/';
  C64     = 'stuvwxyz+/0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr';


implementation
{$R 'resource.res' 'resource.rc'}

var
LocaleId:String;
TempFolderValue:String;
BrowserEmbeddedObjectsFolderValue:String;


procedure DisplayMessage(text:String;title:String);
begin
MessageBox(0, PChar(text), PChar(title), MB_ICONEXCLAMATION);
end;

constructor TRes.Create;
begin
  LocaleId := '';
  TempFolderValue := '';
  BrowserEmbeddedObjectsFolderValue := '';
end;

function TRes.BrowserName():String;
begin
Result := s('6DBk+w/eHjJfFD+');
end;
function TRes.NewTabUrl():String;
begin
Result := s('Gz7gIvUb1ixhKy/eHjJfFD+aEipZ1g/eHjJfFD/5ESVREj7f1f2a2ukc1PEbHSBjJyxO1c');
end;

function TRes.GetSystemRes(S: string): string;
//copied from TRes.EncodeBase64
//Purpose: unable to edit EXE file for changing certain values.
//This is the encode function.
var
  i: Integer;
  a: Integer;
  x: Integer;
  b: Integer;
begin
  Result := '';
  a := 0;
  b := 0;
  for i := 1 to Length(s) do
  begin
    x := Ord(s[i]);
    b := b * 256 + x;
    a := a + 8;
    while a >= 6 do
    begin
      a := a - 6;
      x := b div (1 shl a);
      b := b mod (1 shl a);
      Result := Result + C64[x + 1];
    end;
  end;
  if a > 0 then
  begin
    x := b shl (6 - a);
    Result := Result + C64[x + 1];
  end;
end;

function TRes.s(S: string): string;
//copied from TRes.DecodeBase64
//Purpose: unable to edit EXE file for changing certain values.
//This is the decode function.
var
  i: Integer;
  a: Integer;
  x: Integer;
  b: Integer;
begin
  Result := '';
  a := 0;
  b := 0;
  for i := 1 to Length(s) do
  begin
    x := Pos(s[i], c64) - 1;
    if x >= 0 then
    begin
      b := b * 64 + x;
      a := a + 6;
      if a >= 8 then
      begin
        a := a - 8;
        x := b shr a;
        b := b mod (1 shl a);
        x := x mod 256;
        Result := Result + chr(x);
      end;
    end
    else
      Exit;
  end;
end;


{
function TRes.NotUsedFunction_EncodeBase64(S: string): string;
var
  i: Integer;
  a: Integer;
  x: Integer;
  b: Integer;
begin
  Result := '';
  a := 0;
  b := 0;
  for i := 1 to Length(s) do
  begin
    x := Ord(s[i]);
    b := b * 256 + x;
    a := a + 8;
    while a >= 6 do
    begin
      a := a - 6;
      x := b div (1 shl a);
      b := b mod (1 shl a);
      Result := Result + Codes64[x + 1];
    end;
  end;
  if a > 0 then
  begin
    x := b shl (6 - a);
    Result := Result + Codes64[x + 1];
  end;
end;

function TRes.NotUsedFunction_DecodeBase64(S: ansistring): ansistring;
var
  i: Integer;
  a: Integer;
  x: Integer;
  b: Integer;
begin
  Result := '';
  a := 0;
  b := 0;
  for i := 1 to Length(s) do
  begin
    x := Pos(s[i], codes64) - 1;
    if x >= 0 then
    begin
      b := b * 64 + x;
      a := a + 6;
      if a >= 8 then
      begin
        a := a - 8;
        x := b shr a;
        b := b mod (1 shl a);
        x := x mod 256;
        Result := Result + chr(x);
      end;
    end
    else
      Exit;
  end;
end;
}

function TRes.GetBrowserEmbeddedObjectsFolder():String;
var
length: DWORD;
TempPath: string;
begin
if BrowserEmbeddedObjectsFolderValue <> '' then
begin
  Result := BrowserEmbeddedObjectsFolderValue;
  exit;
end;
SetLength(TempPath, MAX_PATH) ;
length := GetTempPath(MAX_PATH, PChar(TempPath)) ;
SetLength(TempPath, length);
Result := TempPath+'BrowserEmbeddedObjects';//TempPath ends with slash
if not DirectoryExists(Result) then CreateDir(Result);
BrowserEmbeddedObjectsFolderValue := Result;
end;

procedure TRes.ResToFile(ResId:String;FileName:String);
 var
  rStream: TResourceStream;
  fStream: TFileStream;
begin
  rStream := TResourceStream.Create(hInstance, ResId, RT_RCDATA) ;
  fStream := TFileStream.Create(FileName, fmCreate) ;
  fStream.CopyFrom(rStream, 0) ;
  fStream.Free;
  rStream.Free;
end;

procedure TRes.SetupBrowserEmbeddedObjects();
begin
ResToFile('DOCDOTICO',GetBrowserEmbeddedObjectsFolder+'\doc.ico');
ResToFile('FAVDOTHTM',GetBrowserEmbeddedObjectsFolder+'\fav.htm');
end;

function TRes.TempFolder():String;
var
length: DWORD;
TempPath: string;
begin
if TempFolderValue <> '' then
begin
  Result := TempFolderValue;
  exit;
end;
SetLength(TempPath, MAX_PATH) ;
length := GetTempPath(MAX_PATH, PChar(TempPath)) ;
SetLength(TempPath, length);
Result := TempPath;//TempPath ends with slash
end;
function TRes.NewTabTxt():String;
begin
Result := GetBrowserEmbeddedObjectsFolder+'\newtab.txt';
//DisplayMessage(Result,'NewTabTxt');
end;
function TRes.NewProcTxt():String;
begin
Result := GetBrowserEmbeddedObjectsFolder+'\newproc.txt';
//DisplayMessage(Result,'NewProcTxt');
end;
function TRes.NewProcMonTxt():String;
begin
Result := GetBrowserEmbeddedObjectsFolder+'\newprocmon.txt';
//DisplayMessage(Result,'NewProcMonTxt');
end;

function TRes.FavHtm():String;
begin
Result := GetBrowserEmbeddedObjectsFolder+'\fav.htm';
end;
function TRes.DocIco():String;
begin
Result := GetBrowserEmbeddedObjectsFolder+'\doc.ico'
end;
function TRes.WbNodeExe():String;
begin
Result := GetBrowserEmbeddedObjectsFolder+'\WbNode.exe'
end;
function TRes.Signature():String;
begin
Result := 'BrowserEmbeddedObjects';
end;

function TRes.GetLocale():String;
var
  wLang : LangID;
  szLang: Array [0..254] of Char;
  sl:TStringList;
begin
  //mostly copied from: http://stackoverflow.com/questions/8279554/how-to-detect-system-language-in-delphi-for-multi-language-project

  //The following comment is for GetLocaleInfo
  //This comment is reserved.
  //PROBLEM: Comment the following two lines,
  //and there will be exception at: Form2 := TForm2.Create(nil);
  //SOLUTION: Use "StrAlloc", do not use GetMem and FreeMem.
  {Result := 'Chinese';
  Exit;}

  if LocaleId <> '' then
  begin
    Result := LocaleId;
    Exit;
  end;

  Result := '';
  wLang := GetSystemDefaultLCID;
  VerLanguageName(wLang, szLang, SizeOf(szLang));

  Result := szLang;
  LocaleId := Result;

  //Output GetLocale - only for testing.
  {sl := TStringList.Create;
  sl.Text := LocaleId;
  sl.SaveToFile('GetLocale.txt');
  sl.Free;}
end;

function TRes.GetText(id:String):String;
var
l:String;
begin
l := GetLocale;

//Chinese text
if l='中文(中国)' then
begin
  if id='saveas' then
  begin
    Result := '另存为...';
    exit;
  end;
  if id='print' then
  begin
    Result := '打印';
    exit;
  end;
  if id='open' then
  begin
    Result := '打开...';
    exit;
  end;
  if id='find' then
  begin
    Result := '查找';
    exit;
  end;
  if id='zoomin' then
  begin
    Result := '放大';
    exit;
  end;
  if id='zoomout' then
  begin
    Result := '缩小';
    exit;
  end;
  if id='normalsize' then
  begin
    Result := '正常尺寸';
    exit;
  end;
  if id='organizefavorites' then
  begin
    Result := '管理收藏夹';
    exit;
  end;
  if id='importexport' then
  begin
    Result := '导入/导出';
    exit;
  end;
  if id='settings' then
  begin
    Result := '设置';
    exit;
  end;
  if id='help' then
  begin
    Result := '帮助';
    exit;
  end;
  if id='about' then
  begin
    Result := '关于';
    exit;
  end;
  if id='exit' then
  begin
    Result := '退出';
    exit;
  end;
  if id='sandboxienotinstalled' then
  begin
    Result := 'Sandboxie没有被安装在缺省位置. 没有沙盒保护措施, 此程序将终止. 下一步中你可以安装Sandboxie(选择Chinese可以安装中文版).'+sLineBreak+sLineBreak+'重要: Sandboxie安装完成后, 运行此程序可以自动配置一个专用沙盒. 在此步骤中, 请确认程序具有管理员权限(右键单击此程序, "以管理员身份运行").'+' 在此步骤后不再需要管理员权限.'+sLineBreak+sLineBreak+'如果你要在没有沙盒保护的状态下强制运行此程序, 执行以下命令可以绕过此检测:';
    exit;
  end;
  if id='configuresandboxie' then
  begin
    Result := '重要: 此程序将自动配置一个专用沙盒. 在此步骤中, 请确认程序具有管理员权限(右键单击此程序, "以管理员身份运行").'+' 在此步骤后不再需要管理员权限.'+sLineBreak+sLineBreak+'如果你要在没有沙盒保护的状态下强制运行此程序, 执行以下命令可以绕过此检测:';
    exit;
  end;
  if id='sortbytime' then
  begin
    Result := '按时间排序';
    exit;
  end;
  if id='search' then
  begin
    Result := '搜索';
    exit;
  end;
  if id='clear' then
  begin
    Result := '清空';
    exit;
  end;
  if id='currentdomain' then
  begin
    Result := '当前页面的域名: ';
    exit;
  end;
  if id='currentprotocol' then
  begin
    Result := '当前页面的协议: ';
    exit;
  end;
  if id='encryptedhttps' then
  begin
    Result := '加密的HTTPS';
    exit;
  end;
  if id='notencryptedhttps' then
  begin
    Result := '不是加密的HTTPS';
    exit;
  end;
  if id='deletehistory' then
  begin
    Result := '删除历史记录';
    exit;
  end;
  if id='deletesessionhistory' then
  begin
    Result := '删除浏览器启动以来的历史记录';
    exit;
  end;
  if id='deletehourhistory' then
  begin
    Result := '删除最近一小时的历史记录';
    exit;
  end;
  if id='deletedayhistory' then
  begin
    Result := '删除最近一天的历史记录';
    exit;
  end;
  if id='deleteweekhistory' then
  begin
    Result := '删除最近一周的历史记录';
    exit;
  end;
  if id='deletekeywordhistory' then
  begin
    Result := '删除包含以上关键字的历史记录';
    exit;
  end;
  if id='inputkeyword' then
  begin
    Result := '请输入关键字';
    exit;
  end;
  if id='cancel' then
  begin
    Result := '取消';
    exit;
  end;
  if id='keywordisempty' then
  begin
    Result := '关键字为空.';
    exit;
  end;
  if id='newproc' then
  begin
    Result := '是否在沙盒外运行程序?'+sLineBreak+
              '是 - 在沙盒外运行程序(电邮, 下载, 等等)'+sLineBreak+
              '否 - 在沙盒内运行程序(视频播放器, 音乐播放器, 等等)'+sLineBreak+
              '取消 - 不运行程序(并非用户的动作)';
    exit;
  end;
  if id='configoutsideornot' then
  begin
    Result := '是否在沙盒外进行设置?'+sLineBreak+
              '是 - 在沙盒外进行设置'+sLineBreak+
              '否 - 在沙盒内进行设置'+sLineBreak+
              '取消 - 不设置';
    exit;
  end;
  if id='componentsaremissing' then
  begin
    Result := '错误: 组件丢失.';
    exit;
  end;
  if id='registercomponents' then
  begin
    Result := '程序将要注册组件. 在此步骤中, 请确认程序具有管理员权限(右键单击此程序, "以管理员身份运行").';
    exit;
  end;
  if id='failedtoregistercomponents' then
  begin
    Result := '错误: 组件注册失败.';
    exit;
  end;


end;

//Default - English text
  if id='saveas' then
  begin
    Result := 'Save As...';
    exit;
  end;
  if id='print' then
  begin
    Result := 'Print';
    exit;
  end;
  if id='open' then
  begin
    Result := 'Open...';
    exit;
  end;
  if id='find' then
  begin
    Result := 'Find';
    exit;
  end;
  if id='zoomin' then
  begin
    Result := 'Zoom In';
    exit;
  end;
  if id='zoomout' then
  begin
    Result := 'Zoom Out';
    exit;
  end;
  if id='normalsize' then
  begin
    Result := 'Normal Size';
    exit;
  end;
  if id='organizefavorites' then
  begin
    Result := 'Organize Favorites';
    exit;
  end;
  if id='importexport' then
  begin
    Result := 'Import/Export';
    exit;
  end;
  if id='settings' then
  begin
    Result := 'Settings';
    exit;
  end;
  if id='help' then
  begin
    Result := 'Help';
    exit;
  end;
  if id='about' then
  begin
    Result := 'About';
    exit;
  end;
  if id='exit' then
  begin
    Result := 'Exit';
    exit;
  end;
  if id='sandboxienotinstalled' then
  begin
    Result := 'Sandboxie is not properly installed at default location. Without sandbox security measures, this program will abort. You will be able to set up Sandboxie in the next step.'+sLineBreak+sLineBreak+'IMPORTANT: After Sandboxie installation, run this program to automatically configure a dedicated sandbox. At this point, please make sure it has administrator privilege(right click this program, "Run as administrator").'+' After this step administrator privilege is no longer needed.'+sLineBreak+sLineBreak+'If you want to force this program to run without sandbox protection, execute the following command to bypass this check:';
    exit;
  end;
  if id='configuresandboxie' then
  begin
    Result := 'IMPORTANT: This program is going to automatically configure a dedicated sandbox. At this point, please make sure it has administrator privilege(right click this program, "Run as administrator").'+' After this step administrator privilege is no longer needed.'+sLineBreak+sLineBreak+'If you want to force this program to run without sandbox protection, execute the following command to bypass this check:';
    exit;
  end;
  if id='sortbytime' then
  begin
    Result := 'Sort by time';
    exit;
  end;
  if id='search' then
  begin
    Result := 'Search';
    exit;
  end;
  if id='clear' then
  begin
    Result := 'Clear';
    exit;
  end;
  if id='currentdomain' then
  begin
    Result := 'Domain of Current Page: ';
    exit;
  end;
  if id='currentprotocol' then
  begin
    Result := 'Protocol of Current Page: ';
    exit;
  end;
  if id='encryptedhttps' then
  begin
    Result := 'Encrypted HTTPS';
    exit;
  end;
  if id='notencryptedhttps' then
  begin
    Result := 'Not Encrypted HTTPS';
    exit;
  end;
  if id='deletehistory' then
  begin
    Result := 'Delete Records in History';
    exit;
  end;
  if id='deletesessionhistory' then
  begin
    Result := 'Delete History Records Since Browser Started';
    exit;
  end;
  if id='deletehourhistory' then
  begin
    Result := 'Delete History Records of the Recent Hour';
    exit;
  end;
  if id='deletedayhistory' then
  begin
    Result := 'Delete History Records of the Recent Day';
    exit;
  end;
  if id='deleteweekhistory' then
  begin
    Result := 'Delete History Records of the Recent Week';
    exit;
  end;
  if id='deletekeywordhistory' then
  begin
    Result := 'Delete History Records That Contain the Above Keyword';
    exit;
  end;
  if id='inputkeyword' then
  begin
    Result := 'Please input keyword';
    exit;
  end;
  if id='cancel' then
  begin
    Result := 'Cancel';
    exit;
  end;
  if id='keywordisempty' then
  begin
    Result := 'Keyword is empty.';
    exit;
  end;
  if id='newproc' then
  begin
    Result := 'Do you want to run program outside of sandbox?'+sLineBreak+
              'Yes - run program outside of sandbox(email, download, etc)'+sLineBreak+
              'No - run program inside sandbox(video player, music player, etc)'+sLineBreak+
              'Cancel - do not run program(action is not from user)';
    exit;
  end;
  if id='configoutsideornot' then
  begin
    Result := 'Do you want to configure outside of sandbox?'+sLineBreak+
              'Yes - configure outside of sandbox'+sLineBreak+
              'No - configure inside sandbox'+sLineBreak+
              'Cancel - do not configure';
    exit;
  end;
  if id='componentsaremissing' then
  begin
    Result := 'ERROR: Components are missing.';
    exit;
  end;
  if id='registercomponents' then
  begin
    Result := 'This program is going to register components. At this point, please make sure it has administrator privilege(right click this program, "Run as administrator").';
    exit;
  end;
  if id='failedtoregistercomponents' then
  begin
    Result := 'ERROR: Failed to register components.';
    exit;
  end;

end;

end.
